package com.tanhua.server.service;

import com.tanhua.domain.db.UserFreeze;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.dubbo.api.db.UserFreezeApi;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.vo.CommentVo;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsService {

    @Reference
    private UserFreezeApi userFreezeApi;
    @Reference
    private CommentApi commentApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private StringRedisTemplate redisTemplate;

//查询圈子评论
    public ResponseEntity queryCommentsList(String movementId, Integer page, Integer pagesize) {
      PageResult pageResult = commentApi.findAll(page,pagesize,movementId);
        List<Comment> items = (List<Comment>) pageResult.getItems();

        List<CommentVo> list = new ArrayList<>();

        CommentVo commentVo = null;
        for (Comment comment : items) {
            commentVo = new CommentVo();
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());

            commentVo.setNickname(userInfo.getNickname());
            commentVo.setAvatar(userInfo.getAvatar());

            commentVo.setId(comment.getId().toString());
            commentVo.setContent(comment.getContent());
            commentVo.setCreateDate(new DateTime(comment.getCreated()).toString("HH:mm") );
            commentVo.setLikeCount(comment.getLikeCount()); //当前评论的点赞数据
            if(redisTemplate.hasKey("quanzi_publish_like_"+ UserHolder.getUserId() +"_"+comment.getId().toString())){
                commentVo.setHasLiked(1); //当前登录人是否对这个评论点赞
            }else{
                commentVo.setHasLiked(0); //当前登录人是否对这个评论点赞
            }
            list.add(commentVo);
        }
        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);
    }

    //保存对圈子的评论
    public ResponseEntity saveComment(String movementId, String commentContent) {
        Long userId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(2);  //评论类型，1-点赞，2-评论，3-喜欢
        comment.setPubType(1);    //评论内容类型： 1-对圈子动态操作 2-对视频操作 3-对评论操作
        comment.setContent(commentContent);
        comment.setUserId(userId);

        //判断该用户是否冻结评论
      UserFreeze userFreeze= userFreezeApi.findById(userId);
        if (userFreeze!=null) {
            if(userFreeze.getFreezingRange()==2){
                return ResponseEntity.ok(null);
            }
        }

        //返回的是这个圈子动态的点赞数量
        Integer count = commentApi.save(comment);
        return ResponseEntity.ok(count);
    }
//视频评论列表
    public ResponseEntity videosComments(String videoId, Integer page, Integer pagesize) {

        PageResult pageResult = commentApi.findAllVideosComments(page,pagesize,videoId);
        List<Comment> items = (List<Comment>) pageResult.getItems();

        List<CommentVo> list = new ArrayList<>();

        CommentVo commentVo = null;
        for (Comment comment : items) {
            commentVo = new CommentVo();
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());

            commentVo.setNickname(userInfo.getNickname());
            commentVo.setAvatar(userInfo.getAvatar());

            commentVo.setId(comment.getId().toString());
            commentVo.setContent(comment.getContent());
            commentVo.setCreateDate(new DateTime(comment.getCreated()).toString("HH:mm") );
            commentVo.setLikeCount(comment.getLikeCount()); //当前评论的点赞数据
            if(redisTemplate.hasKey("Videos_publish_like_"+ UserHolder.getUserId() +"_"+comment.getId().toString())){
                commentVo.setHasLiked(1); //当前登录人是否对这个评论点赞
            }else{
                commentVo.setHasLiked(0); //当前登录人是否对这个评论点赞
            }
            list.add(commentVo);
        }
        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity fbVideosComments(String publishId, String videoscomment) {
        Long userId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2);  //评论类型，1-点赞，2-评论，3-喜欢
        comment.setPubType(2);    //评论内容类型： 1-对圈子动态操作 2-对视频操作 3-对评论操作
        comment.setContent(videoscomment);
        comment.setUserId(userId);
        //返回的是这个圈子动态的点赞数量
        Integer count = commentApi.save(comment);
        return ResponseEntity.ok(count);
    }
}
